package com.neu.activity;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.viewpager.widget.PagerAdapter;
import androidx.viewpager.widget.ViewPager;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;

import com.neu.MainActivity;
import com.neu.R;

public class WelcomeActivity extends AppCompatActivity {
    private ViewPager viewPager;
    private Button btnWel;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_welcome);
        //初始化对象的方法
        initView();

    }

    private void initView() {
        //对象实例化
        btnWel=findViewById(R.id.btnWel);
        viewPager=findViewById(R.id.viewPager);


        //通过adapter绑定数据
        viewPager.setAdapter(new PagerAdapter() {
            //图片资源
            private int[] imgs={R.drawable.a1,R.drawable.a2,R.drawable.a3,R.drawable.a4,R.drawable.a5};

            @NonNull
            @Override
            public Object instantiateItem(@NonNull ViewGroup container, int position) {
                //创建一个图片对象
                ImageView pic=new ImageView(getApplicationContext());
                pic.setImageResource(imgs[position]);
                pic.setScaleType(ImageView.ScaleType.CENTER_CROP);
                container.addView(pic);
                return pic;
            }

            @Override
            public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
                container.removeView((View) object);
            }

            @Override
            public int getCount() {
                return imgs.length;
            }

            @Override
            public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
                return view==object;
            }
        });

        //viewpager的滑动事件
        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                if(position==4){//第五张图显示
                    btnWel.setVisibility(View.VISIBLE);
                }else{
                    btnWel.setVisibility(View.GONE);
                }
            }

            @Override
            public void onPageSelected(int position) {

            }


            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    //点击按钮进入到Main中
    public void btnClick(View view) {
        Intent intent=new Intent(this, MainActivity.class);
        startActivity(intent);
    }
}